#include "../common/detail.hpp"
#include "../common/message.hpp"

// int main()
// {
//     DLOG("%s","hello");
//     ILOG("%d",1);
//     ELOG("hello world");
//     std::cout << rpc::UUID::uuid() << std::endl;
//     std::cout << rpc::UUID::uuid() << std::endl;
//     std::cout << rpc::UUID::uuid() << std::endl;
//     return 0;
// }

// 测试消息类型
int main()
{
    // 测试rpcrequest
    //  rpc::RpcRequest::ptr rrp = rpc::MessageFactory::create<rpc::RpcRequest>();
    //  rrp->setMethod("Add");
    //  Json::Value val;
    //  val["num1"] = 10;
    //  val["num2"] = 15;
    //  rrp->setParams(val);
    //  std::string str = rrp->serialize();
    //  std::cout << str << std::endl;
    //  rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::Mtype::REQ_RPC);
    //  bool ret = bmp->unserialize(str);
    //  if(ret == false)
    //  return -1;
    //  rpc::RpcRequest::ptr rrp2 = std::dynamic_pointer_cast<rpc::RpcRequest>(bmp);
    //  std::cout << rrp2->method() << std::endl;
    //  std::cout << rrp2->params()["num1"].asInt() << std::endl;
    //  std::cout << rrp2->params()["num2"].asInt() << std::endl;

    // 测试topicrequest
    //  rpc::TopicRequest::ptr trp = rpc::MessageFactory::create<rpc::TopicRequest>();
    //  trp->setTopicKey("news");
    //  //只有主题发布才有消息字段
    //  trp->setOptype(rpc::TopicOptype::TOPIC_PUBLISH);
    //  //trp->setOptype(rpc::TopicOptype::TOPIC_SUBSCRIBE);
    //  trp->setTopicMsg("重大新闻新的一个新闻");
    //  std::string str = trp->serialize();
    //  std::cout << str << std::endl;

    // bool ret = trp->unserialize(str);
    // if(ret == false)
    //     return -1;
    // ret = trp->check();
    // if(ret == false)
    //     return -1;
    // std::cout << trp->topicKey() << std::endl;
    // std::cout << (int)trp->optype() << std::endl;
    // std::cout << trp->topicMsg() << std::endl;

    // 测试serverrequest
    //  rpc::ServiceRequest::ptr srp= rpc::MessageFactory::create<rpc::ServiceRequest>();
    //  srp->setMethod("Add");
    //  //不是服务发现，请求中没有host字段
    //  //srp->setOptype(rpc::ServiceOptype::SERVICE_DISCOVERY);
    //  srp->setOptype(rpc::ServiceOptype::SERVICE_ONLINE);
    //  srp->setHost(rpc::Address("127.0.0.1",9090));

    // std::string str = srp->serialize();
    // std::cout << str << std::endl;
    // rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::Mtype::REQ_SERVICE);
    // bool ret = bmp->unserialize(str);
    // if(ret == false)
    //     return false;
    // ret = bmp->check();
    // if(ret == false)
    //     return false;
    // rpc::ServiceRequest::ptr srp2 = std::dynamic_pointer_cast<rpc::ServiceRequest>(bmp);
    // std::cout << srp2->method() << std::endl;
    // std::cout << (int)srp2->optype() << std::endl;
    // std::cout << srp2->host().first << std::endl;
    // std::cout << srp2->host().second << std::endl;

    // 测试rpcresponse
    //  rpc::RpcResponse::ptr rrp = rpc::MessageFactory::create<rpc::RpcResponse>();
    //  rrp->setResult(33);
    //  rrp->setRcode(rpc::Rcode::RCODE_OK);
    //  std::string str = rrp->serialize();
    //  std::cout << str << std::endl;
    //  rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::Mtype::RSP_RPC);
    //  bool ret = bmp->unserialize(str);
    //  if(ret == false)
    //  return -1;
    //  rpc::RpcResponse::ptr rrp2 = std::dynamic_pointer_cast<rpc::RpcResponse>(bmp);
    //  std::cout << rrp2->result() << std::endl;
    //  std::cout << (int)rrp2->rcode() << std::endl;

    // 测试topicresponse
    //  rpc::TopicResponse::ptr trp = rpc::MessageFactory::create<rpc::TopicResponse>();
    //  trp->setRcode(rpc::Rcode::RCODE_OK);
    //  std::string str = trp->serialize();
    //  std::cout << str << std::endl;
    //  rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::Mtype::RSP_TOPIC);
    //  bool ret = bmp->unserialize(str);
    //  if(ret == false)
    //  return -1;
    //  rpc::TopicResponse::ptr trp2 = std::dynamic_pointer_cast<rpc::TopicResponse>(bmp);
    //  std::cout << (int)trp2->rcode() << std::endl;

    // 测试serverresponse
    rpc::ServiceResponse::ptr srp = rpc::MessageFactory::create<rpc::ServiceResponse>();
    srp->setMethod("Add");
    srp->setOptype(rpc::ServiceOptype::SERVICE_DISCOVERY);
    srp->setRcode(rpc::Rcode::RCODE_OK);
    std::vector<rpc::Address> addrs;
    addrs.emplace_back(rpc::Address("127.0.0.1",9090));
    addrs.emplace_back(rpc::Address("167.0.0.2",8080));
    srp->setHost(addrs);
    std::string str = srp->serialize();
    std::cout << str << std::endl;
    rpc::BaseMessage::ptr bmp = rpc::MessageFactory::create(rpc::Mtype::RSP_SERVICE);
    bool ret = bmp->unserialize(str);
    if (ret == false)
        return -1;
    rpc::ServiceResponse::ptr srp2 = std::dynamic_pointer_cast<rpc::ServiceResponse>(bmp);
    std::cout << srp2->method() << std::endl;
    std::cout << (int)srp2->optype() << std::endl;
    std::cout << (int)srp2->rcode() << std::endl;
    std::vector<rpc::Address> addrv = srp2->host();
    for(auto &addr :addrv)
    {
        std::cout << addr.first << std::endl;
        std::cout << addr.second << std::endl;
    }
}
